<?php

namespace Tasker\Models;

class ProjectModel extends BaseModel
{

	private $id;
	private $project;

	public function find($id)
	{
		$this->id = $id;
		return $this;
	}

	public function getId()
	{
		return $this->id;
	}

	public function getProject()
	{
		if (empty($this->project))
			$this->project = $this->connection->project('id', $this->id)->fetch();
		return $this->project;
	}

	public function getTasks(array $where=array())
	{
		return $this->connection->task('project_id', $this->id)->where($where);
	}

	public function getPrerequisites()
	{
		$ids = $this->getTasks()->select('id');
		return $this->connection->prerequisite_task()->where('task_id IN (' . $ids . ') OR prerequisite_id IN (' . $ids . ')');
	}

	public function getChildren()
	{
		return $this->connection->project('project_id', $this->id);
	}

	public function getProjectTeam()
	{
		return $this->connection->project_member('project_id', $this->id)->order('project_role.priority, user.surname');
	}

	public function getMilestones()
	{
		return $this->connection->milestone('project_id', $this->id)->order('if(isnull(milestone.deadline),1,0),milestone.deadline');
	}

	public function getFiles()
	{
		$types = $this->connection->project_file()->select('file_type.id, file_type.name')->fetchPairs('file_type.id', 'file_type.name');
		$files = array();
		foreach ($types as $id => $name)
		{
			$files[$id]['name'] = $name;
			$files[$id]['files'] = $this->connection->project_file('file_type_id', $id);
		}
		return $files;
	}

	public function getParentId()
	{
		$project = $this->connection->project()->select('project_id')->where('id', $this->id)->fetch();
		return $project['project_id'];
	}

	public function getTaskTypes()
	{
		return $this->connection->project_task_type()->where('project_id', $this->id);
	}

	public function save($values)
	{
		$members = $values['members'];
		$task_types = $values['task_types'];
		unset($values['members'], $values['task_types']);
		if ($values['project_id'] == 0)
			$values['project_id'] = null;

		$this->connection->project[$values['id']]->update((array) $values);
		$this->connection->project_member()->where('project_id', $values['id'])->delete();

		foreach ($members as $id => $member)
		{
			$this->connection->project_member()->insert(array('project_id' => $values['id'], 'user_id' => $id, 'project_role_id' => $member['role']));
		}
		$this->connection->project_task_type()->where('project_id',$values['id'])->delete();
		foreach ($task_types as $task_type)
		{
			$this->connection->project_task_type()->insert(array('project_id'=>$values['id'],'task_type_id'=>$task_type));
		}
	}

	public function delete()
	{
		if ($this->id !== null)
		{
			return $this->connection->project[$this->id]->delete();
		}
	}

	public function createNewProject($values)
	{
		$members = $values['members'];

		unset($values['members']);
		unset($values['id']); // Enable NotORM to get last inserted id
		if ($values['project_id'] == 0)
			$values['project_id'] = null;

		$project = $this->connection->project()->insert((array) $values);
		$this->id = $project['id'];
		foreach ($members as $id => $member)
		{
			$this->connection->project_member()->insert(array('project_id' => $project['id'], 'user_id' => $id, 'project_role_id' => $member['role']));
		}
		return $this;
	}

}
